﻿Imports QLPhuTungOto.Data
Imports QLPhuTungOto.Bus
Public Class frmThemPT

    Dim db As New QLPhuTungOtoDataContext
    Dim PT_BUS As New PhuTungBUS
    Dim LPT_BUS As New LoaiPhuTungBUS
    Dim NCC_BUS As New NhaCungCapBUS
    Dim X_BUS As New XeBUS

    Private Sub frmThemPT_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDSLoaiPT()
        LoadDSNhaCC()
        LoadDSXe()
    End Sub

    Private Sub LoadDSLoaiPT()
        Dim lstLoaiPhuTung As New List(Of LoaiPhuTung)

        lstLoaiPhuTung = LPT_BUS.LayDSLoaiPhuTung()

        Dim loaipt As New LoaiPhuTung With {.MaLoaiPhuTung = -1,
                                           .TenLoaiPhuTung = "< Thêm mới >..."}

        lstLoaiPhuTung.Add(loaipt)

        cbxLoaiPhuTung.DataSource = lstLoaiPhuTung

        cbxLoaiPhuTung.ValueMember = "MaLoaiPhuTung"
        cbxLoaiPhuTung.DisplayMember = "TenLoaiPhuTung"
    End Sub

    Private Sub LoadDSNhaCC()
        Dim lstNhaCC As New List(Of NhaCungCap)

        lstNhaCC = NCC_BUS.LayDSNhaCungCap()

        Dim nhacc As New NhaCungCap With {.MaNhaCungCap = -1,
                                          .TenNhaCungCap = "< Thêm mới >..."}

        lstNhaCC.Add(nhacc)

        cbxNhaCungCap.DataSource = lstNhaCC
        cbxNhaCungCap.ValueMember = "MaNhaCungCap"
        cbxNhaCungCap.DisplayMember = "TenNhaCungCap"
    End Sub

    Private Sub LoadDSXe()
        Dim lstXe As New List(Of Xe)

        lstXe = X_BUS.LayDSXe()

        Dim x As New Xe With {.MaXe = -1,
                              .TenXe = "< Thêm mới >..."}

        lstXe.Add(x)

        cbxLoaiXe.DataSource = lstXe
        cbxLoaiXe.ValueMember = "MaXe"
        cbxLoaiXe.DisplayMember = "TenXe"
    End Sub

    Private Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Close()
    End Sub

    Private Function KiemTraNhap() As String
        Dim err As String = ""
        err += HamHoTro.KiemTraRong(txtMaPhuTung.Text, "Mã phụ tùng")
        err += HamHoTro.KiemTraRong(txtTenPhuTung.Text, "Tên phụ tùng")
        err += HamHoTro.KiemTraRong(txtGiaNhap.Text, "Giá nhập")
        err += HamHoTro.KiemTraRong(txtGiaBan.Text, "Giá bán")
        err += HamHoTro.KiemTraRong(txtDonVi.Text, "Đơn vị")
        If cbxLoaiPhuTung.Items.Count = 1 Then
            err += "Chưa chọn loại phụ tùng" + ControlChars.NewLine
        End If

        If cbxNhaCungCap.Items.Count = 1 Then
            err += "Chưa chọn nhà cung cấp" + ControlChars.NewLine
        End If

        If cbxLoaiXe.Items.Count = 1 Then
            err += "Chưa chọn loại xe" + ControlChars.NewLine
        End If
        Return err
    End Function

    Private Sub ThemPT()
        Dim PT As New PhuTung With {.MaPhuTung = txtMaPhuTung.Text,
                                    .MaLoaiPhuTung = cbxLoaiPhuTung.SelectedValue,
                                    .TenPhuTung = txtTenPhuTung.Text,
                                    .MaNhaCungCap = cbxNhaCungCap.SelectedValue,
                                    .MaHinhAnh = 1,
                                    .SoLuong = 0,
                                    .GiaNhap = Double.Parse(txtGiaNhap.Text),
                                    .GiaBan = Double.Parse(txtGiaBan.Text),
                                    .NgayNhap = Date.Now,
                                    .MaXe = cbxLoaiXe.SelectedValue,
                                    .DonVi = txtDonVi.Text,
                                    .IsDelete = 0}
        PT_BUS.Them(PT)
    End Sub

    Private Sub btnThem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim err As String = ""
        err += KiemTraNhap()
        If err = "" Then
            Try
                ThemPT()
                TrangThai.ThayDoiPhuTung()
                Close()
            Catch ex As Exception
                MessageBox.Show("Không thể thêm phụ tùng")
            End Try
        Else
            MessageBox.Show(err)
        End If
    End Sub

    Private Sub txtGiaBan_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtGiaBan.KeyPress
        If Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Then
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub txtGiaNhap_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtGiaNhap.KeyPress
        If Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Then
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub cbxLoaiPhuTung_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxLoaiPhuTung.Click
        If cbxLoaiPhuTung.Items.Count = 1 Then
            Dim frm As New frmThemLoaiPhuTung
            frm.ShowDialog()
            LoadDSLoaiPT()
        End If
    End Sub

    Private Sub cbxLoaiPhuTung_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxLoaiPhuTung.SelectedIndexChanged
        If cbxLoaiPhuTung.Items.Count > 1 And cbxLoaiPhuTung.SelectedIndex = cbxLoaiPhuTung.Items.Count - 1 Then
            Dim frm As New frmThemLoaiPhuTung
            frm.ShowDialog()
            LoadDSLoaiPT()
        End If
    End Sub

    Private Sub cbxNhaCungCap_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxNhaCungCap.Click
        If cbxNhaCungCap.Items.Count = 1 Then
            Dim frm As New frmThemNhaCungCap
            frm.ShowDialog()
            LoadDSNhaCC()
        End If
    End Sub

    Private Sub cbxNhaCungCap_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxNhaCungCap.SelectedIndexChanged
        If cbxNhaCungCap.Items.Count > 1 And cbxNhaCungCap.SelectedIndex = cbxNhaCungCap.Items.Count - 1 Then
            Dim frm As New frmThemNhaCungCap
            frm.ShowDialog()
            LoadDSNhaCC()
        End If
    End Sub

    Private Sub cbxLoaiXe_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxLoaiXe.Click
        If cbxLoaiXe.Items.Count = 1 Then
            Dim frm As New frmThemXe
            frm.ShowDialog()
            LoadDSXe()
        End If
    End Sub

    Private Sub cbxLoaiXe_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxLoaiXe.SelectedIndexChanged
        If cbxLoaiXe.Items.Count > 1 And cbxLoaiXe.SelectedIndex = cbxLoaiXe.Items.Count - 1 Then
            Dim frm As New frmThemXe
            frm.ShowDialog()
            LoadDSXe()
        End If
    End Sub
End Class